package com.example.dou2204.login;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.dou2204.DouActivity;
import com.example.dou2204.R;
import com.example.dou2204.baseMVVM.BaseActivity;
import com.example.dou2204.databinding.ActivityLoginBinding;
import com.example.dou2204.entity.UserEntity;

public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewModel> {

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    SPUtils.getInstance().put("username",entity.getData().getUsername());
                    SPUtils.getInstance().put("token",entity.getData().getToken());
                    SPUtils.getInstance().put("icon",entity.getData().getIcon());
                    startActivity(new Intent(LoginActivity.this, DouActivity.class));
                    //记录一下当前登录的时间
                    SPUtils.getInstance().put("loginTime",System.currentTimeMillis());
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }
    private AlertDialog dialog;

    @Override
    protected void initView() {
        //获取当前登录时间--如果和记录中的时间差大于规定的时间---我们认为超过了缓存时间，需重新登录
        long time = SPUtils.getInstance().getLong("loginTime");
        if(System.currentTimeMillis()-time<5*60*1000){//7天 7*24*60*60*1000//5分钟 5*60*1000
            startActivity(new Intent(LoginActivity.this, DouActivity.class));
            //记录一下当前登录的时间//重新计时
            SPUtils.getInstance().put("loginTime",System.currentTimeMillis());
        }
        //复选框显示内容添加富文本  举例：文字颜色变化，点击事件
        //获取要修改的字符串
        String string = binding.cb.getText().toString();
        //转成富文本类型
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
        //设置文字颜色
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.RED),6,12, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        //设置点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //直接吐司内容
                ToastUtils.showLong("点击了文字");
                //跳转到具体网页中实现展示--跳转到百度界面
                startActivity(new Intent(LoginActivity.this,XieyiActivity.class));

            }
        };
        spannableStringBuilder.setSpan(clickableSpan,13,19,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        //添加点击事件后，一定记得加这一行，不然没有头效果
        binding.cb.setMovementMethod(LinkMovementMethod.getInstance());
        //将修改后的字符赋值给cb
        binding.cb.setText(spannableStringBuilder);
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etName.getText().toString();
                String password = binding.etPwd.getText().toString();
                //直接提示
                /*if(!binding.cb.isChecked()){
                    ToastUtils.showLong("您未勾选同意隐私的协议");
                    return;
                }*/
                //弹出具体界面来，再次选择
                if(!binding.cb.isChecked()){
                    View inflate = LayoutInflater.from(LoginActivity.this).inflate(R.layout.layout_dialog, null);
                    dialog = new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("提示")
                            .setView(inflate)
                            .create();
                    TextView tvYes = inflate.findViewById(R.id.tv_yes);
                    TextView tvNo = inflate.findViewById(R.id.tv_no);
                    tvYes.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            binding.cb.setChecked(true);
                            viewModel.login(username,password);//同意，直接登录
                            dialog.dismiss();
                        }
                    });
                    tvNo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }else{
                    viewModel.login(username,password);
                }


            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }
}